package show.marvelous.domain.chessgame.aggregate.entity;

import java.util.Random;
import lombok.Getter;
import lombok.NonNull;
import org.jilt.Builder;
import org.jilt.BuilderStyle;
import show.marvelous.domain.chessgame.constans.AirPlanColorEnum;
import show.marvelous.domain.chessgame.constans.PieceCodeEnum;

/**
 * @author Kris
 * 2021/8/27
 */
@Getter
public class DiceRecord{

    public static final Integer MIN_STEP = 1;
    public static final Integer MAX_STEP = 6;
    private static final Random RANDOM_STEP_MAKER = new Random();
    @NonNull
    private final Long id;
    @NonNull
    private final AirPlanColorEnum color;
    @NonNull
    private final Integer step;
    private PieceCodeEnum code;

    @Builder(style = BuilderStyle.TYPE_SAFE)
    public DiceRecord(@NonNull Long id, @NonNull AirPlanColorEnum color, @NonNull Integer step, PieceCodeEnum code){
        this.id = id;
        this.color = color;
        this.step = step;
        this.code = code;
    }

    public DiceRecord(@NonNull Long id, @NonNull AirPlanColorEnum color, @NonNull Integer step){
        this.id = id;
        this.color = color;
        if(step < MIN_STEP || step > MAX_STEP){
            throw new IllegalArgumentException("骰子点数必须是 1 ~ 6 之间的数");
        }
        this.step = step;
    }

    public DiceRecord(@NonNull Long id, @NonNull AirPlanColorEnum color){
        this(id, color, RANDOM_STEP_MAKER.nextInt(MAX_STEP) + 1);
    }

    public PlayRecord play(PieceCodeEnum code){
        if(this.code != null){
            this.code = code;
            return null;
        }else{
            throw new IllegalStateException("该骰子记录已走");
        }
    }

    public boolean isUnused(){
        return this.code == null;
    }
}
